package pokerclient.core;

import java.util.Arrays;

import common.Card;
import common.GameAction;
import common.TrunkatedPlayer;

/**
 * 
 * A class representing a player in the client.
 * 
 */
public class Player implements IPlayer {
	String nickname;
	Card[] hand;
	// An int from 0 to 3 which determines this player's position
	// around the table. When the game starts this is unknown,
	// which is designated by a -1.
	int position = -1;
	GameAction action;
	int stack;
	int amountInPot;

	/**
	 * Constructor
	 */
	public Player() {
		this.nickname = "New Player";
	}

	public Player(String nickname) {
		this.nickname = nickname;
		this.action = GameAction.CONNECT;
	}

	public Player(TrunkatedPlayer trunkPlayer) {
		this(trunkPlayer.getNickname());
		this.position = trunkPlayer.getPosition();
		this.action = trunkPlayer.getAction();
		Card[] newHand = { new Card(), new Card() };
		this.hand = newHand;
		this.amountInPot = trunkPlayer.getAmountInPot();
		this.stack = trunkPlayer.getStack();
	}

	@Override
	public void setHand(Card[] hand) {
		this.hand = hand;
	}

	@Override
	public Card[] getHand() {
		return this.hand;

	}

	@Override
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	@Override
	public String getNickname() {
		return nickname;
	}

	@Override
	public void setPosition(int position) {
		this.position = position;
	}

	@Override
	public int getPosition() {
		return this.position;
	}

	@Override
	public GameAction getAction() {
		return this.action;
	}

	@Override
	public void setAction(GameAction action) {
		this.action = action;
	}

	@Override
	public int getStack() {
		return this.stack;
	}

	@Override
	public int getAmountInPot() {
		return this.amountInPot;
	}

	@Override
	public void setAmountInPot(int amountInPot) {
		this.amountInPot = amountInPot;
	}

	@Override
	public void setStack(int stack) {
		this.stack = stack;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((action == null) ? 0 : action.hashCode());
		result = prime * result + amountInPot;
		result = prime * result + Arrays.hashCode(hand);
		result = prime * result
				+ ((nickname == null) ? 0 : nickname.hashCode());
		result = prime * result + position;
		result = prime * result + stack;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Player other = (Player) obj;
		if (action != other.action)
			return false;
		if (amountInPot != other.amountInPot)
			return false;
		if (!Arrays.equals(hand, other.hand))
			return false;
		if (nickname == null) {
			if (other.nickname != null)
				return false;
		} else if (!nickname.equals(other.nickname))
			return false;
		if (position != other.position)
			return false;
		if (stack != other.stack)
			return false;
		return true;
	}

}
